package com.maszy.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.maszy.bean.Position;
import com.maszy.db.DBConnection;

public class PositionService {
	Position service=new Position(); 
	private static PreparedStatement pst;
	private static ResultSet rs = null;
	public  int add(Position obj) {
		DBConnection con = new DBConnection();
		int result = 0;
		String sql ="insert into Maszy_Position(Maszy_Position_Name,Maszy_Position_Remark) values(?,?)";
		try {
			pst = con.getConnection().prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {			
			pst.setString(1,obj.getPosition_Name());
			pst.setString(2, obj.getPosition_Remark());
			result = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return result;
	}
	public int update(Position obj){
		DBConnection con = new DBConnection();
		int i=0;
		String sql="update Maszy_Position set Maszy_Position_Name=?,Maszy_Position_Remark=? where Maszy_Position_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);			
			pst.setString(1, obj.getPosition_Name());
			pst.setString(2, obj.getPosition_Remark());
			pst.setLong(3, obj.getPosition_Id());
			i=pst.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return i;
	}
	public int delete(Position obj){
		DBConnection con = new DBConnection();
		int i=0;
		String sql="delete from  Maszy_Position  where Maszy_Position_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);	
			pst.setLong(1, obj.getPosition_Id());
			i=pst.executeUpdate();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return i;
	}
	public Position queryById(Position obj){
		DBConnection con = new DBConnection();
		String sql="select * from Maszy_Position where Maszy_Position_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);	
			pst.setLong(1, obj.getPosition_Id());
			rs = pst.executeQuery();
			while (rs.next()) {
				obj=new Position();
				obj.setPosition_Id(rs.getLong("Maszy_Position_Id"));
				obj.setPosition_Name(rs.getString("Maszy_Position_Name"));			
				obj.setPosition_Remark(rs.getString("Maszy_Position_Remark"));	
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return obj;
	}
	public List<Position> queryAll(){
		List<Position> list=new ArrayList<Position>();
		DBConnection con = new DBConnection();
		String sql="select * from Maszy_Position";
		try {
			pst = con.getConnection().prepareStatement(sql);				
			rs = pst.executeQuery();
	
			while (rs.next()) {
				Position obj=new Position();
				obj.setPosition_Id(rs.getLong("Maszy_Position_Id"));
				obj.setPosition_Name(rs.getString("Maszy_Position_Name"));
				obj.setPosition_Remark(rs.getString("Maszy_Position_Remark"));	
				list.add(obj);
			}
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return list;
	}
}
